'''教师系统核心代码'''
from interface import teacher_iface
from lib import common

teacher_func_menu = '''
========== 教师系统 ==========
        1.登录
        2.修改教学课程
        3.查看学员
        4.修改成绩
        q.退出输入【q】
====================================
'''


def teacher_login():
    username, password = common.login_input()
    msg = teacher_iface.login(username=username, password=password)
    print(msg)


@common.auth('teacher')
def select_course():
    flag, school_id = common.select_school()
    if not flag:
        print(school_id)
        return False
    flag, course_id = common.select_course(school_id=school_id)
    if not flag:
        print(course_id)
        return False
    msg = teacher_iface.select_course(course_id=course_id)
    print(msg)


@common.auth('teacher')
def check_student():
    msg = teacher_iface.check_student()
    print(msg)


@common.auth('teacher')
def renew_score():
    check_student()
    choice_stu = input('请输入需要修改成绩的学生姓名:').strip()
    while True:
        new_score = input('请输入修改后的成绩:').strip()
        if not new_score.isdigit():
            print("【请输入数字！】")
        else:
            new_score = int(new_score)
            if new_score < 0 or new_score > 100:
                print('【请输入正确的分数！】')
            else:
                break
    msg = teacher_iface.renew_score(choice_stu=choice_stu, new_score=new_score)
    print(msg)


teacher_menu_dict = {
    '1': teacher_login,
    '2': select_course,
    '3': check_student,
    '4': renew_score
}


def main():
    while True:
        print(teacher_func_menu)
        func_choice = input("请输入功能ID:").strip()
        if func_choice.upper() == 'Q':
            print('【已退出教师系统】')
            break
        elif func_choice not in teacher_menu_dict:
            print('【功能ID有误】')
        else:
            teacher_menu_dict[func_choice]()
